const { Service } = require('egg');
const { tableToList } = require('../utils/tools');


module.exports = class CreditService extends Service {

  /**
   * ! 学分修读情况
   * @return {Array} *
   */
  async table() {
    const Cookie = this.ctx.state.Cookie;
    const apiUrl = `${this.ctx.state.baseUrl}/jsxsd/pyfa/topyfamx`;
    const res = await this.ctx.curl(apiUrl, {
      method: 'GET',
      headers: {
        Cookie,
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
      },
      timeout: 10000,
    });
    if (res.status === 200) {
      return tableToList(res.data.toString(), '#mxh');
    }
    return '';
  }


  /**
   * * 创新学分、素拓分、第二课堂分
   * @return {Array} *
   */
  async extra() {
    const Cookie = this.ctx.state.Cookie;
    const apiUrl = `${this.ctx.state.baseUrl}/jsxsd/pyfa/cxxf_query`;
    console.log('apiUrl', apiUrl);
    const res = await this.ctx.curl(apiUrl, {
      method: 'GET',
      headers: {
        Cookie,
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
      },
      timeout: 10000,
    });
    if (res.status === 200) {
      try {
        return tableToList(res.data.toString(), '#dataList');
      } catch (err) {
        return err;
      }
    }
    return [];
  }
};
